"use strict";
//格式当前时间
var formatTime = function (date,fmt) {
  const year = date.getFullYear()
  const month = date.getMonth()+1
  const day = date.getDate()
  const hour = date.getHours()
  const miute = date.getMinutes()
  const second = date.getSeconds()
  if(fmt=="yyyy-MM-dd"){
    return [year,month,day].map(formatNumber).join('-');
  } else if (fmt =='yyyy-MM-dd HH:mm'){
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, miute].map(formatNumber).join(':');
  } else if (fmt == 'yyyy-MM-dd HH:mm:ss') {
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, miute, second].map(formatNumber).join(':');
  }else if(fmt == 'HH:mm'){
    return [hour, miute].map(formatNumber).join(':');
  }
}
//计算相差时间 年月日 时分
var compute = function (faultDate, completeTime){  
  var stime = new Date(faultDate).getTime();
  var etime = new Date(completeTime).getTime(); 
  var usedTime = etime - stime;  //两个时间戳相差的毫秒数
  var days = Math.floor(usedTime / (24 * 3600 * 1000));
  //计算出小时数
  var leave1 = usedTime % (24 * 3600 * 1000);    //计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  var leave2 = leave1 % (3600 * 1000);        //计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000));
  // var time = days + "天"+hours+"时"+minutes+"分";
  var time = days + ',' + hours + ',' + minutes; 
  return time;
}
const formatNumber = n=>{
  n = n.toString()
  return n[1]?n:'0'+n
}
module.exports={
  formatTime:formatTime,
  compute: compute,
}